function LaserDamage takes nothing returns nothing
	local unit target = GetEnumUnit()
	//--
	if IsUnitEnemy(target , udg_TempPlayer) and IsUnitInRangeLoc(target , udg_TempLocation , udg_TempRadius) and not IsUnitType(target , UNIT_TYPE_STRUCTURE) and GetUnitState(target , UNIT_STATE_LIFE) > 0.0 then
		call UnitDamageTarget(udg_TempDamager , target , udg_TempDamage , true , false , ATTACK_TYPE_NORMAL, DAMAGE_TYPE_COLD , WEAPON_TYPE_METAL_MEDIUM_BASH)
		set udg_TempBoolean = true
	endif
	//--
	set target = null
endfunction

function LaserRiflePathBlock takes nothing returns nothing //如果進入雷射路線區域的部隊是可被攻擊的，則登記下來
	local integer triggerId = GetHandleId(GetTriggeringTrigger())
	local unit enteringUnit = GetTriggerUnit()
	local group inLineGroup = LoadGroupHandle(udg_HT , triggerId , 3)
	local player gunnerOwner = LoadPlayerHandle(udg_HT , triggerId , 4)
	//--
	if IsUnitEnemy(enteringUnit , udg_TempPlayer) and GetUnitState(enteringUnit , UNIT_STATE_LIFE) > 0.0 then
		call GroupAddUnit(inLineGroup , enteringUnit)
	endif
endfunction

function LaserRifleInPath takes nothing returns nothing
	set udg_TempBoolean = true
endfunction

function LaserRifleRoutine takes nothing returns nothing
	local timer laserTimer = GetExpiredTimer()
	local integer timerId = GetHandleId(laserTimer)
	local lightning laserLightning = LoadLightningHandle(udg_HT , timerId , 3)
	local real speed = LoadReal(udg_HT , timerId , 10)
	local real dx = LoadReal(udg_HT , timerId , 4)
	local real dy = LoadReal(udg_HT , timerId , 5)
	local real timeNow = LoadReal(udg_HT , timerId , 6)
	local boolean end = false
	local unit gunner = LoadUnitHandle(udg_HT , timerId , 7)
	local location gunnerLocation = GetUnitLoc(gunner)
	local real gunOffset = LoadReal(udg_HT , timerId , 9)
	local location frontLocation = null
	local real height = GetUnitFlyHeight(gunner) + 60.0
	local group nearUnits = LoadGroupHandle(udg_HT , timerId , 15)
	local rect effectiveRect = LoadRectHandle(udg_HT , timerId , 16)
	local region pathRegion = LoadRegionHandle(udg_HT , timerId , 14)
	local trigger blockTrigger = LoadTriggerHandle(udg_HT , timerId , 12)
	local triggeraction blockTriggerAction = LoadTriggerActionHandle(udg_HT , timerId , 13)
	local boolean hitSomething = false //是否有擊中部隊
	local integer timerCounter = LoadInteger(udg_HT , timerId , 17)
	local item battery = null
	//--
	//設定雷射頭尾位置
	call MoveLocation(gunnerLocation , GetLocationX(gunnerLocation) + dx * gunOffset , GetLocationY(gunnerLocation) + dy * gunOffset)
	set frontLocation = Location(GetLocationX(gunnerLocation) + dx * speed * timeNow , GetLocationY(gunnerLocation) + dy * speed * timeNow)
	
	//造成傷害
	set udg_TempDamager = gunner
	set udg_TempPlayer = GetOwningPlayer(gunner)
	set udg_TempDamage = 10.0
	set udg_TempRadius = 40.0
	set udg_TempLocation = frontLocation
	set udg_TempBoolean = false
	set bj_wantDestroyGroup = true
	call ForGroupBJ(GetUnitsInRangeOfLocMatching(udg_TempRadius+70.0, udg_TempLocation , null) , function LaserDamage)
	set hitSomething = udg_TempBoolean
	
	//檢查鄰近的部隊有無擋住路線
	set udg_TempBoolean = false
	call ForGroupBJ(nearUnits , function LaserRifleInPath)
	if udg_TempBoolean then //有人擋路
		set timeNow = 0.0
		call RemoveRegion(pathRegion)
		set pathRegion = CreateRegion()
		call SaveRegionHandle(udg_HT , timerId , 14 , pathRegion)
		call DestroyGroup(nearUnits)
		set nearUnits = CreateGroup()
		call SaveGroupHandle(udg_HT , timerId , 15 , nearUnits)
		
		call FlushChildHashtable(udg_HT , GetHandleId(blockTrigger))
		call TriggerRemoveAction(blockTrigger , blockTriggerAction)
		call DestroyTrigger(blockTrigger)
		
		set blockTrigger = CreateTrigger()
		call TriggerRegisterEnterRegion(blockTrigger , pathRegion , null)
		set blockTriggerAction = TriggerAddAction(blockTrigger , function LaserRiflePathBlock)
		call SaveTriggerHandle(udg_HT , timerId , 12 , blockTrigger)
		call SaveTriggerActionHandle(udg_HT , timerId , 13 , blockTriggerAction)
		call SaveGroupHandle(udg_HT , GetHandleId(blockTrigger) , 3 , nearUnits)//trigger要知道該group才能加
		call SavePlayerHandle(udg_HT , GetHandleId(blockTrigger) , 4 , GetOwningPlayer(gunner))//trigger要知道該player才能加
	endif
		
	if not hitSomething then
		set timeNow = timeNow + 0.01
	endif
	
	//移動雷射
	call SaveReal(udg_HT , timerId , 6 , timeNow)
	call MoveLocation(frontLocation , GetLocationX( frontLocation ) + dx , GetLocationY( frontLocation ) + dy)
	call MoveLightningEx(laserLightning , true , GetLocationX(gunnerLocation) , GetLocationY(gunnerLocation) , GetLocationZ(gunnerLocation) + height , GetLocationX(frontLocation) , GetLocationY(frontLocation) , GetLocationZ(frontLocation) + height)
	//註冊此rect
	call MoveRectToLoc(effectiveRect , frontLocation)
	call RegionAddRect(pathRegion , effectiveRect)
	
	//扣電量
	set timerCounter = timerCounter + 1
	if timerCounter >= 10 then
		set timerCounter = 0
		set battery = GetBattery(gunner)
		if GetItemCharges(battery) <= 0 then
			call IssueImmediateOrderById(gunner , 851972) //停止
		else
			call SetItemCharges(battery , GetItemCharges(battery) - 1)
		endif
	endif
	call SaveInteger(udg_HT , timerId , 17 ,  timerCounter)
	//--
	set laserTimer = null
	set laserLightning = null
	set gunner = null
	call RemoveLocation(gunnerLocation)
	set gunnerLocation = null
	call RemoveLocation(frontLocation)
	set nearUnits = null
	set effectiveRect = null
	set pathRegion = null
	set blockTrigger = null
	set blockTriggerAction = null
	set battery = null
endfunction

function Trig_LaserRifle_Effect takes nothing returns nothing
	local unit gunner = GetTriggerUnit()
	local location gunnerLocation = GetUnitLoc(gunner)
	local location targetLocation = GetSpellTargetLoc()
	local real angle = AngleBetweenPoints(gunnerLocation , targetLocation)
	local real speed = 2000.0
	local real dx = Cos(angle * bj_DEGTORAD)
	local real dy = Sin(angle * bj_DEGTORAD)
	local lightning laserLightning = null
	local timer laserTimer = CreateTimer()
	local integer timerId = GetHandleId(laserTimer)
	local item battery = GetBattery(gunner)
	local real gunOffset = 75.0
	local real height = GetUnitFlyHeight(gunner) + 60.0
	local real width = 40.0
	//0.0
	local trigger blockTrigger = CreateTrigger()
	local triggeraction blockTriggerAction = null
	local region pathRegion = CreateRegion()
	local group nearUnits = CreateGroup()
	local rect hittingRect = Rect(0.0 , 0.0 , width * 2.0 , width * 2.0)
	//--
	call MoveLocation(targetLocation , GetLocationX(gunnerLocation) + dx * gunOffset , GetLocationY(gunnerLocation) + dy * gunOffset)
	set laserLightning = AddLightningEx("LASE" , true , GetLocationX(targetLocation) , GetLocationY(targetLocation) , GetLocationZ(targetLocation) + height , GetLocationX(targetLocation) , GetLocationY(targetLocation) , GetLocationZ(targetLocation) + height)
	call SetItemCharges( battery , GetItemCharges( battery ) - 1 )
	call SaveLightningHandle( udg_HT , timerId , 3 , laserLightning )
	call SaveReal(udg_HT , timerId , 4 , dx)
	call SaveReal(udg_HT , timerId , 5 , dy)
	call SaveReal(udg_HT , timerId , 6 , 0.0)
	call SaveUnitHandle(udg_HT , timerId , 7 , gunner)
	call SaveReal(udg_HT , timerId , 9 , gunOffset)
	call SaveReal(udg_HT , timerId , 10 , speed)
	call SaveReal(udg_HT , timerId , 11 , 5.0) //最大時間
	//0.0
	call TriggerRegisterEnterRegion(blockTrigger , pathRegion , null)
	set blockTriggerAction = TriggerAddAction(blockTrigger , function LaserRiflePathBlock)
	call SaveTriggerHandle(udg_HT , timerId , 12 , blockTrigger)
	call SaveTriggerActionHandle(udg_HT , timerId , 13 , blockTriggerAction)
	call SaveRegionHandle(udg_HT , timerId , 14 , pathRegion)
	call SaveGroupHandle(udg_HT , timerId , 15 , nearUnits)
	call SaveRectHandle(udg_HT , timerId , 16 , hittingRect)
	call SaveInteger(udg_HT , timerId , 17 , 0)//扣電量用
	
	call SaveGroupHandle(udg_HT , GetHandleId(blockTrigger) , 3 , nearUnits)//trigger要知道該group才能加
	call SavePlayerHandle(udg_HT , GetHandleId(blockTrigger) , 4 , GetOwningPlayer(gunner))//trigger要知道該player才能加
	//0.0
	call TimerStart(laserTimer , 0.01 , true , function LaserRifleRoutine)
	//laser timer
	call SaveTimerHandle(udg_UnitDataHashTable , GetHandleId(gunner) , StringHash("LASER_RIFLE_TIMER") , laserTimer)
	//聲音
	//call PlaySoundOnUnit( gunner , "Units\\Human\\Rifleman\\RiflemanAttack1.wav" )
	call SetUnitAnimationByIndex(gunner , 149)
	//--
	set gunner = null
	call RemoveLocation(gunnerLocation)
	set gunnerLocation = null
	call RemoveLocation(targetLocation)
	set targetLocation = null
	set laserLightning = null
	set laserTimer = null
	set battery = null
	set blockTrigger = null
	set blockTriggerAction = null
	set pathRegion = null
	set nearUnits = null
	set hittingRect = null
endfunction

function Trig_LaserRifle_Channel takes nothing returns nothing
	local unit gunner = GetTriggerUnit()
	local item gun = GetItemOfTypeFromUnitBJ(gunner , 'I00D')
	local item battery = GetBattery(gunner)
	local integer batteryEnergy = GetItemCharges(battery)
	//--	
	if batteryEnergy <= 0 then
		call IssueImmediateOrderById(gunner , 851972) //停止
		call DisplayTimedTextToPlayer(GetOwningPlayer(gunner) , 0 , 0 , 2.0 , "沒有足夠的電力")
	elseif gun == null then
		call UnitRemoveAbility(gunner , 'A012')
	endif
	//--
	set gunner = null
	set gun = null
	set battery = null
endfunction

function LaserRifleStop takes nothing returns nothing
	local unit gunner = GetTriggerUnit()
	local timer laserTimer = LoadTimerHandle(udg_UnitDataHashTable , GetHandleId(gunner) , StringHash("LASER_RIFLE_TIMER"))
	local integer timerId = GetHandleId(laserTimer)
	local lightning laserLightning = LoadLightningHandle(udg_HT , timerId , 3)
	local group nearUnits = LoadGroupHandle(udg_HT , timerId , 15)
	local rect effectiveRect = LoadRectHandle(udg_HT , timerId , 16)
	local region pathRegion = LoadRegionHandle(udg_HT , timerId , 14)
	local trigger blockTrigger = LoadTriggerHandle(udg_HT , timerId , 12)
	local triggeraction blockTriggerAction = LoadTriggerActionHandle(udg_HT , timerId , 13)
	//--
	call PauseTimer(laserTimer)
	call FlushChildHashtable(udg_HT , timerId)
	call DestroyLightning(laserLightning)
	call DestroyGroup(nearUnits)
	call TriggerRemoveAction(blockTrigger , blockTriggerAction)
	call DestroyTrigger(blockTrigger)
	call RemoveRect(effectiveRect)
	call RemoveRegion(pathRegion)
	call DestroyTimer(laserTimer)
	call RemoveSavedHandle(udg_UnitDataHashTable , GetHandleId(gunner) , StringHash("LASER_RIFLE_TIMER"))
	//--
	set gunner = null
	set laserTimer = null
	set laserLightning = null
	set nearUnits = null
	set effectiveRect = null
	set pathRegion = null
	set blockTrigger = null
	set blockTriggerAction = null
endfunction

function Trig_LaserRifle_Conditions takes nothing returns boolean
	return GetSpellAbilityId() == 'A012'
endfunction

function InitTrig_LaserRifle takes nothing returns nothing
	local trigger LaserEffect = CreateTrigger()
	local trigger laserStop = CreateTrigger()
	//--
	set gg_trg_LaserRifle = CreateTrigger()
	call TriggerRegisterAnyUnitEventBJ(gg_trg_LaserRifle , EVENT_PLAYER_UNIT_SPELL_CHANNEL)
	call TriggerAddCondition(gg_trg_LaserRifle , Condition(function Trig_LaserRifle_Conditions))
	call TriggerAddAction(gg_trg_LaserRifle , function Trig_LaserRifle_Channel)
	
	call TriggerRegisterAnyUnitEventBJ(LaserEffect , EVENT_PLAYER_UNIT_SPELL_EFFECT)
	call TriggerAddCondition(LaserEffect , Condition(function Trig_LaserRifle_Conditions))
	call TriggerAddAction(LaserEffect , function Trig_LaserRifle_Effect)
	
	call TriggerRegisterAnyUnitEventBJ(laserStop , EVENT_PLAYER_UNIT_SPELL_ENDCAST )
	call TriggerAddCondition(laserStop , Condition(function Trig_LaserRifle_Conditions))
	call TriggerAddAction(laserStop , function LaserRifleStop)
	//--
	set LaserEffect = null
endfunction